package tq.com.tqteacher.adapt.newPublish;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;

import tq.com.tqcom.net.TQPictureUpload;
import tq.com.tqcom.vh.CommonViewHolder;
import tq.com.tqteacher.R;
import tq.com.tqteacher.adapt.TQJPLeaveMsgAdapter;
import tq.com.tqteacher.model.TQAtachModel;
import tq.com.tqteacher.view.viewhold.TQLeaveMsgViewHold;

public class TQLeaveMsgAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private ArrayList<TQAtachModel> mAtachModels = new ArrayList<>();
    private Type mType;

    private int startNo = 0;//twdy
    public enum Type{
        wk,
        twdy,
        qtfx
    }
    public TQLeaveMsgAdapter(Context pContext, Type pType){
        mContext = pContext;
        mType = pType;
    }

    public void setStartNo(int s){
        startNo = s;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup pViewGroup, final int pI) {
        return CommonViewHolder.sCommonViewHolder(mContext, R.layout.vh_wkleavemsg_layout, pViewGroup)
                .setHolderBinder(new CommonViewHolder.IViewHolder() {
                    @Override
                    public void bind(View pView, Object data) {
                        if(data == null || !(data instanceof TQAtachModel)) return;

                        final TQAtachModel lAtachModel = (TQAtachModel) data;

                        TextView lTitle = pView.findViewById(R.id.title);
                        lTitle.setText(lAtachModel.getDesc());

                        TextView lIndex  = pView.findViewById(R.id.index);
                        int index = mAtachModels.indexOf(data) + 1;

                        String indexStr = null;
                        if(mType == Type.wk){
                            lIndex.setBackgroundResource(R.drawable.wk_title);
                            indexStr = String.format("微课%d", index);
                        }else if(mType == Type.qtfx){
                            lIndex.setBackgroundResource(R.drawable.bg_ly);
                            indexStr = String.format("第%d份", index);
                        }else {
                            lIndex.setBackgroundResource(R.drawable.bg_wt);
                            indexStr = String.format("%s题", lAtachModel.getDesc());

                            lTitle.setText("");
                        }
                        lIndex.setText(indexStr);

                        View lView = pView.findViewById(R.id.leaveMsg);
                        if(lAtachModel.getComment() != null){
                            lView.setVisibility(View.VISIBLE);

                            TQLeaveMsgViewHold lLeaveMsgViewHold = new TQLeaveMsgViewHold(lView);
                            lLeaveMsgViewHold.setComment(lAtachModel.getComment());
                        }else{
                            lView.setVisibility(View.GONE);
                        }

                        final String title = indexStr;
                        pView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if(mIClickListener != null){
                                    mIClickListener.openComment(lAtachModel, title);
                                }
                            }
                        });

                        View line = pView.findViewById(R.id.line);
                        if(mAtachModels.indexOf(data) == (mAtachModels.size() - 1)){
                            line.setVisibility(View.GONE);
                        }else {
                            line.setVisibility(View.VISIBLE);
                        }
                    }
                });
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder pViewHolder, int pI) {
        if(pViewHolder instanceof CommonViewHolder){
            (((CommonViewHolder) pViewHolder)).bindData(mAtachModels.get(pI));
        }
    }

    @Override
    public int getItemCount() {
        return mAtachModels.size();
    }

    public void clear(){
        mAtachModels.clear();
        notifyDataSetChanged();
    }
    public void append(ArrayList<TQAtachModel> pTQAtachModels){
        mAtachModels.addAll(pTQAtachModels);
        notifyDataSetChanged();
    }

    public void update(TQAtachModel pModel){
        int pos = mAtachModels.indexOf(pModel);
        if(pos >= 0){
            notifyItemChanged(pos);
        }
    }

    public void setIClickListener(IClickListener pIClickListener){
        mIClickListener = pIClickListener;
    }
    private IClickListener mIClickListener;
    public interface IClickListener{
        void openComment(TQAtachModel pModel, String title);
    }
}
